﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Quartz;

namespace Max.PH4_2102A.Shopping.API.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Route("quartz")]
    [ApiController]
    public class QuartzNetController : ControllerBase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get()
        {
            //Scheduler 调度器
            var sch = await new Quartz.Impl.StdSchedulerFactory().GetScheduler();
            sch.Start();

            //获取自己定义的任务（工作）
            var job = Quartz.JobBuilder.Create<MyJob>().Build();

            //触发器
            var trigger = TriggerBuilder.Create().WithCronSchedule("2 * * * * ? *").Build();

            //将三个对象，连接在一起
            await sch.ScheduleJob(job, trigger);

            return Ok(0);
        }
    }


    //利用IJob 定义你自己的事件
    public class MyJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            SendEmailService sendEmail = new SendEmailService();

            return Task.Factory.StartNew(() =>
            {
                //你自己要做的一些事情 
                sendEmail.Send();
            });
        }
    }

    public class SendEmailService
    {
        public void Send()
        {
            //具体逻辑....
            Console.WriteLine($"发送成功--{DateTime.Now.ToString()}");
        }
    }
}
